apiready = initApiready;

function initApiready() {

    ajaxConfig();

    historybackup();

    keepScreen.off();

    if(typeof apireadyMap == 'object' && apireadyMap instanceof Array) {
        apireadyMap.forEach(function(value, index, array) {
            typeof value == 'function' ? value() : void(0);
        });
    }
};

// open new frame
function openNewFrame(name, url) {
    if(typeof api != 'undefined') {
        api.execScript({
            name: 'root',
            script: 'createNewFrame("' + name + '", "' + url + '");'
        });
    }
}

// 后退或退出
function historybackup() {
    var domGoback = document.querySelector('.action-goback');
    if(domGoback) {
        domGoback.onclick = function() {
            // goback
            window.history.back();
        }
    }
    var domClose = document.querySelector('.action-close');
    if(domClose) {
        domClose.onclick = function() {
            var name = this.getAttribute('data-frame');
            var redirect = this.getAttribute('data-redirect');
            if(typeof api != 'undefined') {
            		if (redirect) {
            			openNewFrame(name, redirect);
            		} else {
            			api.execScript({
	                    name: 'root',
	                    script: 'closeAFrame("' + name + '")'
	                });
            		}
            }
        }
    }
}

// jude goback or exit or cancelAjax
function keybackCallback() {
    var domGoback = document.querySelector('.action-goback');
    if(domGoback) {
        domGoback.click();
        return;
    }
    var domClose = document.querySelector('.action-close');
    if(domClose) {
        domClose.click();
        return;
    }
    api.closeWidget();
}

// 跳转页面
function repage(url) {
    window.location.replace(url);
}

// app切换到前台
var listenerResumeCallback = {
    queue: [],
    init: function() {
        var self = listenerResumeCallback;
        for(var i in self.queue) {
            if(typeof self.queue[i] == 'function') {
                self.queue[i]();
            }
        }
    }
};

// 切换后台
var listenerPauseCallback = {
    queue: [],
    init: function() {
        var self = listenerPauseCallback;
        for(var i in self.queue) {
            if(typeof self.queue[i] == 'function') {
                self.queue[i]();
            }
        }
    }
};

// 阻止页面休眠
var keepScreen = {
    on: function() {
        if(typeof api != 'undefined') {
            api.setKeepScreenOn({
                keepOn: true
            });
        }
    },
    off: function() {
        if(typeof api != 'undefined') {
            api.setKeepScreenOn({
                keepOn: false
            });
        }
    }
};

//配置ajax状态码
function ajaxConfig() {
    ajax.config.host('http://sg.51hub.cn');
    ajax.config.data({
        device: api.deviceId
    });
    ajax.config.state({
        200: true,
        201: true,
        204: true
    });
    ajax.config.status({
        403: function(res) {
            modal.tips(res.error, function() {
                window.location.href = 'login.html';
            });
            return false;
        },
        500: function(res) {
            //$("body").html(res.data);
            return false;
        }
    });
}